package com.shuhe.metastore;

import com.alibaba.fastjson.JSON;
import com.shuhe.JettyServer;
import com.shuhe.common.CommonFunction;
import com.shuhe.common.Tools;
import com.shuhe.domain.mxgc.SqlResponseResultMeta;
import org.apache.log4j.Logger;
import org.sqlite.jdbc4.JDBC4Connection;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class DBSql {

    private static Logger logger = Logger.getLogger(DBSql.class);

    public static String url = JettyServer.globalProperties.getProperty("db.url");
    private static String username = JettyServer.globalProperties.getProperty("db.user");
    private static String password = JettyServer.globalProperties.getProperty("db.password");
    private static String driver = JettyServer.globalProperties.getProperty("db.driver");
    public static String dbType = JettyServer.globalProperties.getProperty("db.type");


    public static ResultSet querySql(Connection conn, String sql) throws Exception {
        return conn.createStatement().executeQuery(sql);
    }


    public static Connection connect() throws Exception {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }


    public static Connection connectByUrl(String url) throws Exception {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static Connection connectByAll(String url, String username, String password, String driver) throws Exception {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static int execSql(Connection conn, String sql) throws Exception {
        //logger.info(sql);
        return conn.createStatement().executeUpdate(sql);
    }



    public static int batchExecSqlByParameter(Connection conn, String sql, ArrayList<String> datas,
                                              ArrayList<SqlResponseResultMeta> metas) throws Exception {
        //  logger.info(sql);
        conn.setAutoCommit(false);
        PreparedStatement ps = conn.prepareStatement(sql);
        for (int i = 0; i < datas.size(); i++) {
            String json = datas.get(i);
            HashMap map = JSON.parseObject(json, HashMap.class);
            for (int j = 0; j < metas.size(); j++) {
                SqlResponseResultMeta meta = metas.get(j);
                ps.setObject(j + 1, map.get(meta.getName()));
            }
            ps.addBatch();
            if (i % 1000 == 0) {
                ps.executeBatch();
                ps.clearParameters();
            }
        }
        ps.executeBatch();
        conn.commit();
        return 0;
    }

    public static int batchExecSql(Connection conn, ArrayList<String> sqls) throws Exception {
        try{
            Statement stat = conn.createStatement();
            for (int i = 0; i < sqls.size(); i++) {
                String sql = sqls.get(i);
                //logger.info(sql);
                stat.addBatch(sql);
            }
            stat.executeBatch();
            return 0;
        }catch (Exception e){
            logger.info(conn);
            logger.info(sqls.get(0));
            throw e;
        }

    }
}
